﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZkSurvey.BLL.SurveyMgr;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.Web.Helper;

namespace ZkSurvey.Web.VP.SurveyResult
{
    public partial class Summary : System.Web.UI.UserControl
    {
        QuestionBl questionBl = new QuestionBl();
        SurveyBl surveyBl = new SurveyBl();
        ExternalUserBl extUserBl = new ExternalUserBl();
        AnswerBl answerBl = new AnswerBl();
        ParticipationBl particBl = new ParticipationBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            //根据是否传进来参数ExternalUserId 来判断是查看问卷还是查看问卷结果
            if (!IsPostBack)
            {
                LoadInfo();

                BindQuestions();
            }
        }

        private void LoadInfo()
        {
            int? svId = UrlHelper.GetQueryStringID();
            ViewState["SurveyId"] = svId.Value;

            Survey survey = surveyBl.GetSurvey(svId.Value);
            //ltrStartTime.Text = survey.StartTime.ToString("yyyy-MM-dd");
            //ltrEndTime.Text = survey.EndTime.ToString("yyyy-MM-dd");
            ltrStatus.Text = survey.StatusTitle;
            ltrTotalParticsCount.Text = particBl.GetParticipationConut(svId.Value).ToString();
            ltrValidParticsCount.Text = particBl.GetValidParticipationConut(svId.Value).ToString();
            ltrInValidParticsCount.Text = particBl.GetInValidParticipationConut(svId.Value).ToString();
            ltrUndefiedParticsCount.Text = particBl.GetUndifiedParticipationConut(svId.Value).ToString();
        }

        private void BindQuestions()
        {
            int svId = Convert.ToInt32(ViewState["SurveyId"]);

            List<Question> questions = questionBl.GetQuestionList(svId).FindAll(q => q.Type != QuestionType.Input);
            rptQuestions.DataSource = questions;
            rptQuestions.DataBind();

            Survey survey = surveyBl.GetSurvey(svId);
            ltrQuesCount.Text = questions.Count.ToString();
            ltrSvTitle.Text = survey.Title;
            ltrSvDesciption.Text = survey.Description;
        }

        protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Question question = e.Item.DataItem as Question;

                Literal ltrQueTitle = e.Item.FindControl("ltrQuesTitleFormat") as Literal;
                ltrQueTitle.Text = string.Format("{0}.{1}", question.Sort, question.Title);

                Repeater rptOptions = e.Item.FindControl("rptOptions") as Repeater;
                rptOptions.DataSource = question.Options;
                rptOptions.DataBind();
            }
        }

        protected void rptOptions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Option option = e.Item.DataItem as Option;

                Literal ltrOptionCount = e.Item.FindControl("ltrOptionCount") as Literal;
                //查询含有这个选项的答题个数，仅限有效问题
                ltrOptionCount.Text = answerBl.GetValidQuestionAnswerList(option.QuestionId).Count(a => !string.IsNullOrEmpty(a.Result) && a.Result.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Contains(option.Id.ToString())).ToString();
            }
        }

    }
}